using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UCAamControl : Control
    {
        private bool isAlarm = false;
        private Color alarmColor = Color.Red;
        private bool isFlashing = false;
        private Timer flashTimer;

        [Category("行为")]
        [Description("是否报警")]
        public bool IsAlarm
        {
            get { return isAlarm; }
            set 
            {
                isAlarm = value;
                if (isAlarm && isFlashing)
                {
                    flashTimer.Start();
                }
                else
                {
                    flashTimer.Stop();
                }
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("报警颜色")]
        public Color AlarmColor
        {
            get { return alarmColor; }
            set 
            {
                alarmColor = value;
                this.Invalidate();
            }
        }

        [Category("行为")]
        [Description("是否闪烁")]
        public bool IsFlashing
        {
            get { return isFlashing; }
            set 
            {
                isFlashing = value;
                if (isAlarm && isFlashing)
                {
                    flashTimer.Start();
                }
                else
                {
                    flashTimer.Stop();
                }
            }
        }

        private bool showAlarm = true;

        public UCAamControl()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            this.Size = new Size(30, 30);

            flashTimer = new Timer();
            flashTimer.Interval = 500;
            flashTimer.Tick += FlashTimer_Tick;
        }

        private void FlashTimer_Tick(object sender, EventArgs e)
        {
            showAlarm = !showAlarm;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            int baseWidth = this.Width / 2;
            int baseHeight = this.Height / 5;
            int baseX = (this.Width - baseWidth) / 2;
            int baseY = this.Height - baseHeight;

            // 绘制底座
            using (Brush baseBrush = new SolidBrush(Color.Gray))
            {
                g.FillRectangle(baseBrush, baseX, baseY, baseWidth, baseHeight);
            }
            using (Pen pen = new Pen(Color.Black, 1))
            {
                g.DrawRectangle(pen, baseX, baseY, baseWidth, baseHeight);
            }

            // 绘制灯罩
            int lampWidth = baseWidth * 2 / 3;
            int lampHeight = this.Height - baseHeight - 5;
            int lampX = (this.Width - lampWidth) / 2;
            int lampY = 5;

            using (Pen pen = new Pen(Color.Black, 1))
            {
                g.DrawEllipse(pen, lampX, lampY, lampWidth, lampHeight);
            }

            // 绘制灯泡
            if (isAlarm && showAlarm)
            {
                using (Brush brush = new SolidBrush(alarmColor))
                {
                    g.FillEllipse(brush, lampX + 2, lampY + 2, lampWidth - 4, lampHeight - 4);
                }
            }
        }
    }
}